package com.cakeclient.clientsoap.contract;


import org.ksoap2.serialization.NullSoapObject;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

public class ProductType extends ContractObject {
    public int id;
    public String name;
    public String illustration;
    public String description;
    //    public ProductCategory[] parent;
    public SoapArray<Float> localPrices;
    public Float recommendedPriceTTC;
    public Float vat;

    public ProductType() {
    }

    public ProductType(com.cakeclient.Model.ProductType productType) {
        if (productType.getRemoteID() != null)
            id = Integer.valueOf(productType.getRemoteID().toString());
        name = productType.getName();
        description = productType.getDescription();
        localPrices = new SoapArray<Float>("localPrices");
        for (HashMap<Long, com.cakeclient.Model.LocalPriceHT> entry : productType.getLocalPriceHashMap().values()) {
            for (com.cakeclient.Model.LocalPriceHT localPriceHT : entry.values()) {
                localPrices.add(new Float(localPriceHT.getValue()));
            }
        }
        if (productType.getRecommendedPriceHashMap().get(null) != null)
            recommendedPriceTTC = productType.getRecommendedPriceHashMap().get(null).getValue();
        vat = productType.getVat();
    }

    public void fill(SoapObject soapObject) {
        if (!(soapObject.getPropertySafely("id") instanceof NullSoapObject))
            this.id = Integer.parseInt(soapObject.getPropertyAsString("id"));
        if (!(soapObject.getPropertySafely("name") instanceof NullSoapObject))
            this.name = soapObject.getPropertySafelyAsString("name");
        if (!(soapObject.getPropertySafely("illustration") instanceof NullSoapObject))
            this.illustration = soapObject.getPropertySafelyAsString("illustration");
        if (!(soapObject.getPropertySafely("description") instanceof NullSoapObject))
            this.description = soapObject.getPropertySafelyAsString("description");

//        SoapObject parentSoap = soapObject.getPropertySafely("parent") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("parent");
//        if (parentSoap != null) {
//            parent = new ProductCategory[parentSoap.getPropertyCount()];
//            for (int i = 0; i < parentSoap.getPropertyCount(); i++) {
//                SoapObject aParentSoap = (SoapObject) parentSoap.getProperty(i);
//                parent[i] = new ProductCategory();
//                parent[i].fill(aParentSoap);
//            }
//        }

//        SoapObject localPricesSoap = soapObject.getPropertySafely("localPrices") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("localPrices");
//        if (localPricesSoap != null) {
//            localPrices = new SoapArray<Float>(localPricesSoap.getPropertyCount(), "localPrices");
//            for (int i = 0; i < localPricesSoap.getPropertyCount(); i++) {
//                SoapObject aParentSoap = (SoapObject) localPricesSoap.getProperty(i);
//                localPrices.add(i, new Float());
//                localPrices.get(i).fill(aParentSoap);
//            }
//        }
        if (!(soapObject.getPropertySafely("recommendedPriceTTC") instanceof NullSoapObject))
            this.recommendedPriceTTC = Float.parseFloat(soapObject.getPropertySafelyAsString("recommendedPriceTTC"));
        if (!(soapObject.getPropertySafely("vat") instanceof NullSoapObject))
            this.vat = Float.parseFloat(soapObject.getPropertySafelyAsString("vat"));

    }
}
